#include"Block_queue.hpp"

void* Consumer(void* arg)
{
    Block_queue::block_queue<int> *bq = static_cast<Block_queue::block_queue<int>*>(arg);
    while(true){
        sleep(1);
        int data;
        bq->pop(&data);
        std::cout<<"消费者拿到数据: "<<data<<std::endl;
        
    }
}
void* Producer(void* arg)
{
    Block_queue::block_queue<int> *bq = static_cast<Block_queue::block_queue<int>*>(arg);
    int data = 0;
    while(true){
        data++;
        bq->push(data);
        // std::cout<<"生产者生产数据: "<<data<<std::endl;
        printf("%ld 生产数据: %d\n", pthread_self(),data);
    }
}
int main()
{
    Block_queue::block_queue<int> *bq = new Block_queue::block_queue<int>();
    // pthread_t consumer_tid, producer_tid;
    // pthread_create(&consumer_tid, nullptr, Consumer, bq);
    // pthread_create(&producer_tid, nullptr, Producer, bq);
    pthread_t producer[5], consumer[5];
    for(int i = 0; i < 5; i++){
        pthread_create(&producer[i], nullptr, Producer, bq);
        pthread_create(&consumer[i], nullptr, Consumer, bq);
    }
    for(int i = 0; i < 5; i++){
        pthread_join(producer[i], nullptr);
        pthread_join(consumer[i], nullptr);
    }

    // pthread_join(consumer_tid, nullptr);
    // pthread_join(producer_tid, nullptr);
    delete bq;
    return 0;
}